/**
 * 质控病种列表初始化
 */
var SdsQcentity = {
    id: "entityTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    entityId: 0
};

SdsQcentity.onClickEntity = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体病种。");
        return false;
    }
    SdsQcentity.entityId = treeNode.id;
    //重置中间区域
    SdsQcentityItem.seItem = null;
    SdsQcentityItem.needStatus = null;
    SdsQcentityItem.updateStatus = null;
    SdsQcentityItem.entityItemId = 0;
    $(".hisui-checkbox").checkbox("uncheck")
    //查询质控项目
    SdsQcentityItem.search();
};

/**
 * 质控项目表管理初始化
 */
var SdsQcentityItem = {
    id: "SdsQcentityItemTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    entityItemId: null,
    needStatus:null,
    updateStatus:null
};

/**
 * 初始化表格的列
 */
SdsQcentityItem.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '质控病种版本', field: 'versionId', align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '代码', field: 'btCode', width: 150, align: 'left', valign: 'middle', sortable: true},
        {title: '描述', field: 'btDesc', width: 300, align: 'left', valign: 'middle', sortable: true},
        {title: '上传类型id', field: 'btUpType', width: 100, align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '上传类型', field: 'dictionary.description', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '是否必填', field: 'btIsNeeded', width: 80, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '更新标记', field: 'updateFlag', width: 80, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '有效标志', field: 'btIsActive', width: 80, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '关联项目', field: 'formItemCode', width: 150, align: 'center', valign: 'middle', sortable: true},
        {title: '值域字典', field: 'itemDicCnt', width: 80, align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
                var _html = "";
                if(value > 0) {
                    _html += '<a href="javascript:SdsQcentityItem.toEItemDic(\''+row.id+'\',\''+row.btDesc+'\')" title="查看"><span style="color: green">查看</span></a>';
                } else {
                    _html += '<a href="javascript:SdsQcentityItem.toEItemDic(\''+row.id+'\',\''+row.btDesc+'\')" title="添加">添加</a>';
                };
                return _html;
            }},
        {title: '操作', field: 'id', width: 80, align: 'center', valign: 'middle',formatter:function(value,row,index){
                var _html = "";
                _html += '<a href="javascript:SdsQcentityItem.synchro(\''+row.id+'\')" title="同步"><i class="hisui-icon icon-transfer"></i></a>';
                if(row.updateFlag == 1) {
                    _html += '<a href="javascript:SdsQcentityItem.reviewUpdates(\'' + row.id + '\')" title="审核"><i class="hisui-icon icon-apply-check"></i></a>';
                };
                return _html;
            }}];
};

/**
 * 检查是否选中
 */
SdsQcentityItem.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsQcentityItem.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增质控项目表
 */
SdsQcentityItem.add = function () {
    if (SdsQcentity.entityId) {
        var index = Sys.open('新增质控项目表',Sys.backendPath + '/sds/sdsqcentityitem/to_add/' + SdsQcentity.entityId,['800px', '420px']);
        this.layerIndex = index;
    } else {
        Sys.info("请先选中某一病种！");
    }
};

/**
 * 打开查看质控项目表详情
 */
SdsQcentityItem.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('质控项目表详情',Sys.backendPath + '/sds/sdsqcentityitem/to_update/' + SdsQcentity.entityId +'/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 作废质控项目表
 */
SdsQcentityItem.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/sds/sdsqcentityitem/setInvalidStatus", function (data) {
	            Sys.success("作废成功!");
	            SdsQcentityItem.table.refresh();
	        }, function (data) {
	            Sys.error("作废失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("sdsQcentityItemId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否作废该质控项目?",operation);
    }
};

/**
 * 弹出值域字典
 */
SdsQcentityItem.toEItemDic = function (eitemId,eitemName) {
    var index = Sys.open('['+eitemName+']值域字典',Sys.backendPath + '/sds/sdsqceitemdic/' + eitemId,['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 审核项目信息
 */
SdsQcentityItem.reviewUpdates = function(id){
    var review = function() {
        var ajax = new $ax(Sys.backendPath + "/sds/sdsqcentityitem/resetUpdateFlag", function (res) {
            Sys.info("审核成功！")
            SdsQcentityItem.table.refresh();
        }, function (res) {
            Sys.info("审核失败！")
        });
        ajax.set("sdsQcentityItemId", id);
        ajax.start();
    }
    Sys.confirm("请确认该项目是否已同步？",review);
}

/**
 * 同步关联表单项目信息
 */
SdsQcentityItem.synchro = function (id) {
    var params = {};
    if (SdsQcentity.entityId) {
        params.entityId = SdsQcentity.entityId;
        if (!id) {
            id = null;
        }
        params.entityItemId = id;
        var synchro = function(){
            Sys.ajax(Sys.backendPath + "/sds/sdsqcentityitem/synchro",params,function(data){
                Sys.success("同步成功!");
            }, function () {
                Sys.success("同步失败!");
            },"json",true,"项目数据正在同步中..");
        };
        Sys.confirm("是否同步标准项目至表单项目?",synchro);
    } else {
        Sys.info("请先选中某一病种！");
    }
}

/**
 * 筛选有效项目或者更新项目
 */
SdsQcentityItem.filter = function (){
    SdsQcentityItem.needStatus = $("#isNeeded").checkbox("getValue")?1:null
    SdsQcentityItem.updateStatus = $("#isUpdated").checkbox("getValue")?1:null
    SdsQcentityItem.search();
}

/**
 * 查询质控项目表列表
 */
SdsQcentityItem.search = function () {
    var queryData = {};
    queryData['entityId'] = SdsQcentity.entityId;
    queryData['btIsNeeded'] = SdsQcentityItem.needStatus;
    queryData['updateFlag'] = SdsQcentityItem.updateStatus;
    SdsQcentityItem.table.refresh({query: queryData});
};

$(function () {
    //查询病种树
    var ztree = new $ZTree("entityTree", Sys.backendPath + "/sds/sdsqcentity/tree");
    ztree.bindOnClick(SdsQcentity.onClickEntity);
    ztree.init();
    SdsQcentity.ztree = ztree;
    fuzzySearch('entityTree','#seachKey',null,true);
    //查询并初始化质控项目表格
    var itemColunms = SdsQcentityItem.initColumn();
    var itemtable = new BSTable(SdsQcentityItem.id, Sys.backendPath + "/sds/sdsqcentityitem/list", itemColunms);
    itemtable.setPaginationType("server");
    itemtable.setShowExport(false);
    itemtable.setShowRefresh(false);
    itemtable.setShowToggle(false);
    itemtable.setShowColumns(false);
    itemtable.setQueryParams({"entityId":0});
    SdsQcentityItem.table = itemtable.init();
});
